function RemoveSpaces(str) {
    return str.replace(/\s+/g, "");
}

function cutOut(str) {
    return str.substring(0, 29);
}

function search() {
    var detail = cutOut(RemoveSpaces($("#detail").val()));
    var select = $("#select").val();
    SearchCheck(detail, select);

}

function SearchCheck(detail, select) {
    if (detail == "" && select != "全部") {
        alert("内容不能为空！");
        return false;
    }
    if (select == "ISBN") {
        if (isNaN(detail)) {
            alert("ISBN必须为数字！");
            return false;
        }
    }
    window.close();
    window.open("/queryBook?detail=" + detail + "&select=" + select);

}


function more(contentID) {

    if ($("#hidden" + contentID).val() == "more") {
        $("#hidden" + contentID).val("less");
        $("#detailConnect" + contentID).height("auto");
        $("#detail" + contentID).height("auto");
        $("#detail" + contentID).html(bookList[contentID].detail);
        $("#book" + contentID).height($("#detail" + contentID).height() + 120);
        $("#space" + contentID).html("");
        $("#more" + contentID).html("收起");
    } else {
        $("#hidden" + contentID).val("more");
        $("#detailConnect" + contentID).height("70px");
        $("#detail" + contentID).height("70px");
        $("#detail" + contentID).html(bookList[contentID].detail.substring(0, 115));
        $("#book" + contentID).height("160px");
        $("#space" + contentID).html("...");
        $("#more" + contentID).html("查看更多");
    }
}


function writeList(bookList) {
    var str = "";
    for (var i = 0; i < bookList.length; i++) {
        str += "<div class=book id=book" + i + ">";
        str += "<div class=bookImg>";
        str += "<a href=/detail?bookId=" + bookList[i].bookID + ">";
        str += "<img src=static/img/" + bookList[i].pic + ".jpg> </a></div>";

        str += "<div class=info>";

        str += "<div class=title>";
        str += "<a href=/detail?bookId=" + bookList[i].bookID + "> <span>《</span><span>" + bookList[i].bookName + "</span> <span>》</span></a></div>";
        str += "<div class=press>";
        str += "<span style='color: #6a6a6a'>出版社：</span>";
        str += "<span>" + bookList[i].press + "</span>";
        str += "<span style='margin-left: 30px;color: #6a6a6a'>作者：</span><span >" + bookList[i].author + "</span>";
        str += "<span style='margin-left: 30px;color: #6a6a6a'>ISBN：</span><span >" + bookList[i].isbn + "</span>";
        str += "</div>";

        str += "<div class=type>";
        str += "<span style='color: #6a6a6a'>主题：</span>";
        str += "<span>" + bookList[i].type1 + "</span>";
        if (bookList[i].type2 != 0)
            str += "、<span>" + bookList[i].type2 + "</span>";
        if (bookList[i].type3 != 0)
            str += "、<span>" + bookList[i].type3 + "</span>";
        str += " </div>";

        <!--截取115个字-->
        str += "<div class=detail id=detailConnect" + i + ">";
        str += "<span style='color: #2d2c2c;font-size: 18px'>简介：</span>"
        str += "<span id=detail" + i + ">" + bookList[i].detail.substring(0, 115) + "</span>";
        if (bookList[i].detail.length > 115)
            str += "<span id=space" + i + ">...</span>"
        str += " <a target=_blank style='float: right' href='' onclick='toRemand("+bookList[i].bookID+")'>借阅</a>"
        if (bookList[i].detail.length > 115)
            str += "<a href=javascript:void(0); onclick='more(" + i + ")'style='float: right' id=more" + i + ">查看更多</a>";
        str += "</div>";
        str += " </div>";
        str += "</div>";
        str += "<input type='hidden' value='more' id=hidden" + i + ">"
    }
    $("#bookContent").html(str);

}


function change() {
    clear();
    state = $("#state").val();
    resetCode();
    if (state == "login") {
        $("#state").attr("value", "register");
        $("#top").html("欢迎注册");
        $("#radio").css("display", "block");
        $("#submit").css("margin-top", "5px");
        $("#submit").attr("value", "注册");
        $("#h5msg").html("已有账号");
        $("#amsg").text("点击登录");
        $("#pwd1").css("display", "block");



    }
    if (state == "register") {

        $("#state").attr("value", "login");
        $("#top").html("请登录");
        $("#radio").css("display", "none");
        $("#submit").css("margin-top", "30px");
        $("#submit").attr("value", "登录");
        $("#h5msg").html("没有有账号");
        $("#amsg").html("点击注册");
        $("#pwd1").css("display", "none");
    }
}

function LoginCheck(admin) {
    state = $("#state").val();
    var name = $("#name").val();
    var pwd = $("#pwd").val();
    var sex = $('input[name="sex"]:checked').val();
    var code = $("#code").val().toLowerCase();
    if (name == "") {
        alert("请输入用户名！");
        return false;
    }
    if (name.indexOf(" ") == 0) {
        alert("用户名不能有空格!");
        return false;
    }
    if (pwd == "") {
        alert("请输入密码！");
        return false;
    }
    if (pwd.indexOf(" ") == 0) {
        alert("密码不能有空格!");
        return false;
    }
    if(state=="register"){
        var pwd1 = $("#pwd1").val();
        if(pwd1==""){
            alert("请确认密码！");
            return false
        }
        if(pwd1 != pwd){
            alert("密码输入不一致请再次输入！");
            $("#pwd1").val("");
            return false;
        }
    }

    if (code == "") {
        alert("请输入验证码！");
        return false;
    }


    if (code != verVal.toLowerCase()) {
        alert("验证码输入错误！");
        $("#code").val("");
        $("#code").focus();
        resetCode();
        return false;
    }


    var userLoginUrl = "/user/login?name=" + name + "&pwd=" + pwd;
    var userRegisterUrl = "/user/register?name=" + name + "&pwd=" + pwd +"&sex=" + sex;


    var adminLoginUrl = "/admin/login?name=" + name + "&pwd=" + pwd;
    var adminRegisterLoginUrl = "/admin/register?name=" + name + "&pwd=" + pwd;
    if(admin==1) {
        if (state == "register") {
            register(adminRegisterLoginUrl,admin,name,pwd);
        } else {
            login(adminLoginUrl,admin);
        }
    }else{
        if (state == "register") {
            register(userRegisterUrl,admin,name,pwd);
        } else {
            login(userLoginUrl,admin);
        }
    }

}

function login(url,admin) {
    $.get(
        url,
        function (date) {
            if(date!="-1") {
                setCookie(date);
                window.close();
                if(admin==1) {
                    window.open("/bookIO/toAllIOBook");
                }
                else
                    window.open("/index");
            }
            else {
                alert("用户名或者密码错误！");
                $("#code").val("");
                resetCode();
            }
        }
    )
}

function register(url,admin,name,pwd) {
    $.get(
        url,
        function (date) {
            if(date!="-1"){
                var userLoginUrl = "/user/login?name=" + name + "&pwd=" + pwd;
                login(userLoginUrl,admin);
                window.close();
            }
            else{
                alert("用户名已存在！");
                clear();
                $("#code").val("");
                resetCode();
            }
        }
    )
}

function clear() {
    $("#name").val("");
    $("#pwd").val("");
    $("#code").val("");
}

function setCookie(cvalue){
    document.cookie="id="+cvalue+";path=/";
}

function getCookie() {
    var id = document.cookie.substring(3)
    if(id!="")
        return id;
    return "";
}

function userSpace(){
    if(getCookie("id"))
        alert("请登录！");
    else
        window.open("个人中心");

}

function toRemand(bookid){
    var readerId = getCookie();
    if(readerId==""){
        if(confirm("登录后可借阅是否登录?"))
            window.open("/login/toLogin");
    }
    else{
        window.open("/Lend/allBook?bookid="+bookid+"&readerid="+readerId);
    }
}


















